﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BALayer;

namespace PGBT
{
    public partial class FormBanHang : Form
    {
        private GiaoDich dbGD = null;
        private HangHoa dbHH = null;
        private DataSet dsHangHoa = null; // id, tenhang, dongia, soluongcon
        private DataSet dsKH = null;

        private bool khachHangCu = false;

        public FormBanHang()
        {
            InitializeComponent();

            dbGD = new GiaoDich();
            dbHH = new HangHoa();
        }

        private void gdvHangHoa_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void LoadData()
        {
            dsHangHoa = dbHH.DanhSachHangHienCo();
            cbTenHang.DataSource = dsHangHoa.Tables[0];
            cbTenHang.DisplayMember = "tenhang";
        }

        private void FormBanHang_Load(object sender, EventArgs e)
        {
            cbID.Visible = false;
            txtID.Visible = true;

            KhachHang dbKH = new KhachHang();
            dsKH = dbKH.DanhSachKhachHang();
            cbID.DataSource = dsKH.Tables[0];
            cbID.DisplayMember = "id";

            LoadData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(dsHangHoa.Tables[0].Rows[cbTenHang.SelectedIndex].ItemArray[1].ToString());
            string tenhang = dsHangHoa.Tables[0].Rows[cbTenHang.SelectedIndex].ItemArray[2].ToString();
            double dongia = Convert.ToDouble(dsHangHoa.Tables[0].Rows[cbTenHang.SelectedIndex].ItemArray[4].ToString());
            int soluong = 0;

            if (txtSoLuong.Text == "")
                MessageBox.Show("Nhập số lượng !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                soluong = Convert.ToInt32(txtSoLuong.Text);

            dgvHangHoa.Rows.Add(id, tenhang, dongia, soluong);
        }

        private void cbTenHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtDonGia.Text = dsHangHoa.Tables[0].Rows[cbTenHang.SelectedIndex].ItemArray[4].ToString();
        }

        private void txtSoLuong_TextChanged(object sender, EventArgs e)
        {
            int soluong = Convert.ToInt32(txtSoLuong.Text);
            int soluongcon = Convert.ToInt32(dsHangHoa.Tables[0].Rows[cbTenHang.SelectedIndex].ItemArray[3].ToString());

            if (soluong > soluongcon)
            {
                MessageBox.Show("Hàng hóa đã chọn không có đủ !\nSố lượng còn là: " + soluongcon, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSoLuong.Text = Convert.ToString(soluongcon);
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            dgvHangHoa.Rows.RemoveAt(dgvHangHoa.CurrentCell.RowIndex);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string error = "";
            bool f = false;
            int makh = -1;

            if (!khachHangCu)
            {
                KhachHang dbKH = new KhachHang();
                makh = dbKH.ThemKhachHang(ref error, txtName.Text, txtAddress.Text, txtPhoneNumber.Text);

                if(makh == -1)
                {
                    MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                this.txtID.Text = Convert.ToString(makh);
            }
            else
            {
                makh = Convert.ToInt32(dsKH.Tables[0].Rows[cbID.SelectedIndex].ItemArray[0].ToString());
            }

            dbGD.BeginTransaction();

            try
            {
                int mahoadon = dbGD.TaoHoaDon(ref error, makh, DateTime.Now);

                if(mahoadon == -1)
                    throw new Exception("không thể tạo hóa đơn !");

                for (int i = 0; i < dgvHangHoa.Rows.Count - 1; i++)
                {
                    int mahang = Convert.ToInt32(dgvHangHoa.Rows[i].Cells[0].Value.ToString());
                    int soluong = Convert.ToInt32(dgvHangHoa.Rows[i].Cells[3].Value.ToString());
                    int dongia = Convert.ToInt32(dgvHangHoa.Rows[i].Cells[2].Value.ToString());

                    if (!dbGD.ThemHangVaoHoaDon(ref error, mahoadon, mahang, soluong, dongia))
                    {
                        throw new Exception(error);
                    }
                }

                dbGD.CommitTransaction();
                MessageBox.Show("Giao dịch thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Close();
            }
            catch (Exception ex)
            {
                dbGD.RollbackTransaction();
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if(khachHangCu == true)
            {
                txtName.ReadOnly = false;
                txtAddress.ReadOnly = false;
                txtPhoneNumber.ReadOnly = false;                

                khachHangCu = false;
                txtID.Visible = true;
                cbID.Visible = false;
            }
            else
            {
                txtName.ReadOnly = true;
                txtAddress.ReadOnly = true;
                txtPhoneNumber.ReadOnly = true;

                khachHangCu = true;
                txtID.Visible = false;
                cbID.Visible = true;
            }
        }

        private void cbID_SelectedIndexChanged(object sender, EventArgs e)
        {   
            if(khachHangCu)
            {
                txtName.Text = dsKH.Tables[0].Rows[cbID.SelectedIndex].ItemArray[1].ToString();
                txtAddress.Text = dsKH.Tables[0].Rows[cbID.SelectedIndex].ItemArray[2].ToString();
                txtPhoneNumber.Text = dsKH.Tables[0].Rows[cbID.SelectedIndex].ItemArray[3].ToString();
            }
        }
    }
}
